/**
 * 
 */
package insight.google.game.era.data;

import java.util.Collection;
import java.util.Map;
import java.util.LinkedHashMap;
import java.util.Iterator;

import com.google.appengine.api.users.User;

/**
 * @author MMC
 *
 */
public class Host extends BaseData {
	private int state;
	private String hostID;
	private final static String DEFAULT_ROOM_NAME = "Room";
	private final static int DEFAULT_ROOM_NUMBERS = 10;
	
	private Map<String, Player> playerMap;
	private Map<String, Room> roomMap;
	
	public Host() {
		hostID = "Era Host";
		playerMap = new LinkedHashMap<String, Player>();
		roomMap = new LinkedHashMap<String, Room>();
		makeRoom(DEFAULT_ROOM_NUMBERS);
	}
	
	private boolean makeRoom(int roomNumber) {
		if (roomMap.isEmpty()) {
			String roomName;
			for (int i = 1; i<= roomNumber; i++) {
				roomName = DEFAULT_ROOM_NAME + i;
				roomMap.put(roomName, new Room(roomName));
			}
			return true;
		} else {
			return false;
		}
	}
	
	public Iterator<Room> getRooms() {
		return roomMap.values().iterator();
	}
	
	public boolean joinRoom(String roomName, Player player) {
		Room room = roomMap.get(roomName);
//		if (room != null) {
//			return room.join(player);
//		}
		return false;
	}
	
	public boolean join(Player player) {
		String playerID = player.getUserID();
		player.setHost(this);
		return (playerMap.put(playerID, player) == null) ? true : false; 
	}
	
	public void quit(Player player) {
		String playerID = player.getUserID();
		playerMap.remove(playerID);
	}
	
	public String toString() {
		return hostID;
	}
}
